<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends BaseController {
	public function __construct() {
		parent::__construct();
		$this->make_login();//全部确认需要登录之后进行
	}
	
    public function index(){
        $this->display();
    }

    public function account(){
    	$account = M('account');
    	$cur_data = strtotime(date('Y-m-d 00:00:00'));
    	//$data = date("Y-m-d 24:59:59");
    	//echo $data;
    	//echo $cur_data;
    	//exit();
    	$map['day'] = array('egt',$cur_data);
    	$count 	= $account->where($map)->count();
    	$page_class = new \Think\Page($count, 10);
		$pageshow = $page_class->show(); // 分页显示输出
		$list = $account->order('accountid desc')->where($map)->select();
		$moneyall = $account->where($map)->sum('money');
		$this->assign('money',$moneyall);
		$this->assign('list' , $list); // 赋值数据集
		$this->assign('page' , $pageshow); // 赋值分页输出
    	$this->display();
    }

    public function account_do(){
    	$account = M('account');
    	$data['goodname'] = I('post.goodname','');
		$data['amount'] = I('post.amount', '');
		$data['money'] = I('post.money');
		$data['day'] = time();
		if($account->add($data)) {
			$this->success('添加账单成功', '/User/account');
		} else {
			$this->error($mod_user->getError(), '/User/account');
		}
    }

    public function account_edit(){
    	$account = M('account');
    	$id = I('get.id', 0, 'intval');
    	$where['accountid'] = array('eq', $id);
    	$info = $account->where($where)->select();
    	$this->assign('info', $info);
    	$this->display();
    }

    public function account_edit_do(){
    	$account = M('account');
    	$where['accountid'] = I('post.accountid','');
    	$data['goodname'] = I('post.goodname','');
		$data['amount'] = I('post.amount', '');
		$data['money'] = I('post.money');
		$admin = $account->where($where)->save($data);
		if($admin){
			$this->success('修改成功', '/User/account');
		}else{
			$this->error('修改失败', '/User/account_edit');
		}
    }

    public function account_drop(){
		$accountid = I('get.id', 0, 'intval');
		if(M('account')->where('accountid=' . $accountid)->delete()) {
			$this->success('删除成功', '/User/account');
		} else {
			$this->error('发生未知错误,请联系管理员', '/User/account');
		}
	}

	public function service(){
		$service = M('service');
    	$cur_data = strtotime(date('Y-m-d 00:00:00'));
    	$map['day'] = array('egt',$cur_data);
    	$count 	= $service->where($map)->count();
    	$page_class = new \Think\Page($count, 10);
		$pageshow = $page_class->show(); // 分页显示输出
		$list = $service->order('serviceid desc')->where($map)->select();
		$this->assign('list' , $list); // 赋值数据集
		$this->assign('page' , $pageshow); // 赋值分页输出
    	$this->display();
	}

	public function service_do(){
		$account = M('service');
    	$data['version'] = I('post.version','');
		$data['phonenum'] = I('post.phonenum', '');
		$data['client'] = I('post.client');
		$data['remark'] = I('post.remark');
		$data['day'] = time();
		if($account->add($data)) {
			$this->success('维修信息已提交', '/User/service');
		} else {
			$this->error($account->getError(), '/User/service');
		}
	}

	public function service_edit(){
		$service = M('service');
    	$id = I('get.id', 0, 'intval');
    	$where['serviceid'] = array('eq', $id);
    	$info = $service->where($where)->select();
    	$this->assign('info', $info);
    	$this->display();
	}

	public function service_edit_do(){
		$service = M('service');
    	$where['serviceid'] = I('post.serviceid','');
    	$data['version'] = I('post.version','');
		$data['phonenum'] = I('post.phonenum', '');
		$data['clientname'] = I('post.clientname');
		$data['remark'] = I('post.remark');
		$admin = $service->where($where)->save($data);
		if($admin){
			$this->success('修改成功', '/User/service');
		}else{
			$this->error('修改失败', '/User/service_edit');
		}
	}

	public function service_drop(){
		$serviceid = I('get.id', 0, 'intval');
		if(M('service')->where('serviceid=' . $serviceid)->delete()) {
			$this->success('删除成功', '/User/service');
		} else {
			$this->error('发生未知错误,请联系管理员', '/User/service');
		}
	}

	public function usercode(){
		$user = M('user');
		$userinfo = $user->where(array('userid' => $this->user_info['userid']))->select();
		$this->assign('userinfo',$userinfo);
		$this->display();
	}

	public function usecode_do(){
		$user = M('user');
		$where['userid'] = I('post.userid', '');
		$data['usercode'] = md5(I('post.usercode', ''));
		$usercode = $user->where($where)->save($data);
		if($usercode){
			session('user_info', null);
			$this->success('修改密码成功,请重新登录','/Com/login');
		}else{
			$this->error('修改密码失败，请联系管理员', '/User/idnex');
		}
	}

	public function usercenter(){
		$user = M('user');
		$userinfo = $user->where(array('userid' => $this->user_info['userid']))->select();
		$this->assign('userinfo', $userinfo);
		$this->display();
	}

	public function usercenter_do(){
		$user = M('user');
		$data['phone'] = I('post.phone','');
		$data['qq'] = I('post.qq','');
		$data['turename'] = I('post.turename','');
		$data['address'] = I('post.address','');
		$userinfo = $user->where(array('userid' => $this->user_info['userid']))->save($data);
		if($userinfo){
			$this->success('您的信息已经保存','/User/index');
		}else{
			$this->error('信息更新失败，请联系管理员','/User/index');
		}
	}

	public function addbaby(){
		$user = M('user');
		$category = M('category');
		$where['cateparent'] = 0;
		$catelist = $category->where($where)->order(array('cateparent'=>'asc'))->select();
		$userinfo = $user->where(array('userid' => $this->user_info['userid']))->select();
		$this->assign('catelist', $catelist);
		$this->assign('userinfo', $userinfo);
		$this->display();
	}

	public function addbaby_do(){
		$date['babyname']= I('post.babyname','');
        $date['category']= I('post.category','');
        $date['babymoney']= I('post.babymoney','');
        $date['phone']= I('post.phone','');
        $date['qq']= I('post.qq','');
        $date['clientname']= I('post.clientname','');
        $date['address']= I('post.address','');
        $date['babycont']= I('post.babycont','');
        $date['stunum']= I('post.stunum','');
        $date['picurl']= I('post.picurl','');
        $date['picurl1']= I('post.picurl1','');
        $date['picurl2']= I('post.picurl2','');
        $date['stunum']= I('post.stunum','');
        $date['day']= time();
        $goods = M('goods');
        if($addgoods = $goods->add($date)){
            $this->success('发布宝贝成功', '/User/babylist');
        }else{
            $this->error('发布宝贝失败', '/User/index');
        }
	}

	public function babylist(){
		$goods = M('goods');
    	$count 	= $goods->where(array('stunum' => $this->user_info['username']))->count();
    	$page_class = new \Think\Page($count, 10);
		$pageshow = $page_class->show(); // 分页显示输出
		$babylist = $goods->order('goodsid desc')->where(array('stunum' => $this->user_info['username']))->select();
		$this->assign('babylist' , $babylist); // 赋值数据集
		$this->assign('page' , $pageshow); // 赋值分页输出
    	$this->display();
	}

	public function babylist_edit(){
		$this->display();
	}

	public function babylist_drop(){
		$goodsid = I('get.id', 0, 'intval');
		if(M('goods')->where('goodsid=' . $goodsid)->delete()) {
			$this->success('删除成功', '/User/babylist');
		} else {
			$this->error('发生未知错误,请联系管理员', '/User/babylist');
		}
	}
}